from flask import Blueprint, request, render_template

search = Blueprint('search', __name__, url_prefix='/search')




@search.route('/<int:page>/', methods=['GET', 'POST'])
def search_view(page):
    # request.args  # get方法
    # request.form  # post 获取
    # request.values  # get和post获取
    print('page',page)
    from book项目 import Book
    word = request.values.get('word', '')
    books = Book.query.filter(Book.name.contains(word))

    pagination = books.paginate(page=page, per_page=1, error_out=False)
    if page > pagination.pages:
        pagination = books.paginate(page=1, per_page=1, error_out=False)

    books = pagination.items

    # page 获取数据的页数
    # per_page 每一页的数据量
    print(pagination.total)  # 总共获取多少个数据
    print(pagination.pages)  # 一共有多少页
    print(pagination.prev_num)  # # 上一页
    print(pagination.next_num)  # # 下一页

    range_page = [i for i in range(1, pagination.pages+1)]
    return render_template('search.html', books=books, pagination=pagination,
                           range_page=range_page)
